/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sisgraf.util;
import com.mysql.jdbc.CommunicationsException;
import java.sql.SQLRecoverableException;
import org.hibernate.HibernateException;
import sisgraf.model.Unidade;
import sisgraf.model.Cor;
import sisgraf.model.Material;
import sisgraf.model.Vendedor;
import sisgraf.model.Modulo;
import sisgraf.model.Funcao;
import sisgraf.model.Perfil;
import sisgraf.model.Acesso;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.SessionFactory;
import sisgraf.model.Usuario;
import sisgraf.model.Cliente;
import sisgraf.model.Orcamento;
import sisgraf.model.ItemOrcamento;
import sisgraf.model.Pedido;
import sisgraf.model.ServicoItemOrcamento;
import sisgraf.model.FichaCliente;
/**
 * Hibernate Utility class with a convenient method to get Session Factory object.
 *
 * @author Jordan Guerra
 */
public class HibernateUtil {
    private static SessionFactory sessionFactory;
    private HibernateUtil() {
    }
    public static SessionFactory getSessionFactory() {
        if (sessionFactory == null) {
            try {
                // Create the SessionFactory from standard (hibernate.cfg.xml)
                // config file.
                AnnotationConfiguration ac = new AnnotationConfiguration();
                ac.addAnnotatedClass(Unidade.class);
                ac.addAnnotatedClass(Cor.class);
                ac.addAnnotatedClass(Material.class);
                ac.addAnnotatedClass(Vendedor.class);
                ac.addAnnotatedClass(Modulo.class);
                ac.addAnnotatedClass(Funcao.class);
                ac.addAnnotatedClass(Perfil.class);
                ac.addAnnotatedClass(Acesso.class);
                ac.addAnnotatedClass(Usuario.class);
                ac.addAnnotatedClass(Cliente.class);
                ac.addAnnotatedClass(Orcamento.class);
                ac.addAnnotatedClass(ItemOrcamento.class);
                ac.addAnnotatedClass(ServicoItemOrcamento.class);
                ac.addAnnotatedClass(Pedido.class);
                sessionFactory = ac.configure().buildSessionFactory();
//            } catch (CommunicationsException ex) {
//                // Log the exception.
//                System.err.println("Initial SessionFactory creation failed." + ex);
//                throw new ExceptionInInitializerError(ex);
            } catch (HibernateException ex) {
                // Log the exception.
                System.err.println("Initial SessionFactory creation failed." + ex);
                throw new ExceptionInInitializerError(ex);
            } catch (RuntimeException ex) {
                // Log the exception.
                System.err.println("Initial SessionFactory creation failed." + ex);
                throw new ExceptionInInitializerError(ex);
            } catch (Exception ex) {
                // Log the exception.
                System.err.println("Initial SessionFactory creation failed." + ex);
                throw new ExceptionInInitializerError(ex);
            } catch (Throwable ex) {
                // Log the exception.
                System.err.println("Initial SessionFactory creation failed." + ex);
                throw new ExceptionInInitializerError(ex);
            }
            return sessionFactory;
            
        } else {
            return sessionFactory;
        }

    }

//    public static void main(String[] args) {
//        HibernateUtil.getSessionFactory();
//    }

}
